package subject_set.lc200;

/**
 * @author haomin
 * @date 2022/12/26 20:23
 **/
public class Num25 {
    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            if(head == null || head.next == null) return head;
            ListNode dummyHead = new ListNode(-1), pre = dummyHead, cur = head, next;
            dummyHead.next = head;
            int size = 0, count = 0;
            while(head != null) {size++; head = head.next;}
            count = size / k;
            while(count-- > 0) {
                for(int i = 0; i < k-1; ++i) {
                    next = cur.next;
                    cur.next = next.next;
                    next.next = pre.next;
                    pre.next = next;
                }
                pre = cur;
                cur = pre.next;
            }
            return dummyHead.next;
        }
    }
}